﻿// <copyright file="DocumentationPage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-26</date>
// <summary>Documentation Page.</summary>

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Xml.Linq;

    /// <summary>
    /// Documentation Page
    /// </summary>
    public partial class DocumentationPage : UserControl
    {
        #region Member Data
        /// <summary>
        /// Resource name
        /// </summary>
        private string resourceName;
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the DocumentationPage class
        /// </summary>
        public DocumentationPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the DocumentationPage class
        /// </summary>
        /// <param name="resourceName">the name of the resource</param>
        public DocumentationPage(string resourceName) :
            this()
        {
            this.ResourceName = resourceName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the Resouce name
        /// </summary>
        public string ResourceName
        {
            get
            {
                return this.resourceName;
            }

            set
            {
                this.ReadResource(value);

                this.resourceName = value;
            }
        }
        #endregion

        #region Implementation

        /// <summary>
        /// Read a resource
        /// </summary>
        /// <param name="resourceName">the resource name</param>
        private void ReadResource(string resourceName)
        {
            using (var stream = this.GetType().Assembly.GetManifestResourceStream(resourceName))
            {
                using (var reader = new StreamReader(stream))
                {
                    var text = reader.ReadToEnd();

                    var doc = XDocument.Parse(text);

                    AssemblyName.Text = doc.Descendants("assembly").Elements("name").First().Value;

                    var members = from member in doc.Descendants("member")
                                  where member.Attribute("name").Value.StartsWith("T:", StringComparison.OrdinalIgnoreCase)
                                  select new Member
                                  {
                                      Prefix = "T",
                                      Name = member.Attribute("name").Value.Substring(2),
                                      Summary = member.Element("summary").Value.Trim(),
                                      Properties = from typeMember in doc.Descendants("member")
                                                   where typeMember.Attribute("name").Value.StartsWith("P:" + member.Attribute("name").Value.Substring(2), StringComparison.OrdinalIgnoreCase)
                                                   select new Member
                                                   {
                                                       Prefix = "P",
                                                       Name = typeMember.Attribute("name").Value.Substring(member.Attribute("name").Value.Length + 1),
                                                       Summary = typeMember.Element("summary").Value.Trim()
                                                   },
                                      Methods = from typeMember in doc.Descendants("member")
                                                where typeMember.Attribute("name").Value.StartsWith("M:" + member.Attribute("name").Value.Substring(2), StringComparison.OrdinalIgnoreCase)
                                                select new Member
                                                {
                                                    Prefix = "M",
                                                    Name = typeMember.Attribute("name").Value.Substring(member.Attribute("name").Value.Length + 1),
                                                    Summary = typeMember.Element("summary").Value.Trim(),
                                                    Parameters = from param in typeMember.Elements("param")
                                                             select new Parameter
                                                             {
                                                                 Name = param.Attribute("name").Value,
                                                                 Description = param.Value
                                                             },
                                                }
                                  };

                    Types.ItemsSource = members;
                }
            }
        }
        #endregion
    }
}
